// This file is part of Notepad4.
// See License.txt for details about distribution and modification.
//! Lexer for Groovy, Gradle.

#include <cassert>
#include <cstring>

#include <string>
#include <string_view>
#include <vector>

#include "ILexer.h"
#include "Scintilla.h"
#include "SciLexer.h"

#include "WordList.h"
#include "LexAccessor.h"
#include "Accessor.h"
#include "StyleContext.h"
#include "CharacterSet.h"
#include "StringUtils.h"
#include "LexerModule.h"
#include "LexerUtils.h"

using namespace Lexilla;

namespace {

struct EscapeSequence {
	int outerState = SCE_GROOVY_DEFAULT;
	int digitsLeft = 0;

	// highlight any character as escape sequence.
	bool resetEscapeState(int state, int chNext) noexcept {
		if (state == SCE_GROOVY_DOLLAR_SLASHY || (state == SCE_GROOVY_SLASHY_STRING && chNext != '/') || IsEOLChar(chNext)) {
			return false;
		}
		outerState = state;
		digitsLeft = 1;
		if (chNext == 'u') {
			digitsLeft = 5;
		}
		return true;
	}
	bool atEscapeEnd(int ch) noexcept {
		--digitsLeft;
		return digitsLeft <= 0 || !IsHexDigit(ch);
	}
};

enum {
	GroovyLineStateMaskLineComment = 1, // line comment
	GroovyLineStateMaskImport = 1 << 1, // import
};

//KeywordIndex++Autogenerated -- start of section automatically generated
enum {
	KeywordIndex_Keyword = 0,
	KeywordIndex_Type = 1,
	KeywordIndex_Class = 3,
	KeywordIndex_Interface = 4,
	KeywordIndex_Enumeration = 5,
	KeywordIndex_Constant = 6,
	MaxKeywordSize = 24,
};
//KeywordIndex--Autogenerated -- end of section automatically generated

enum class DocTagState {
	None,
	At,				// @param x
	InlineAt,		// {@link package.class#member label}
	TagOpen,		// <tag>
	TagClose,		// </tag>
};

enum class KeywordType {
	None = SCE_GROOVY_DEFAULT,
	Annotation = SCE_GROOVY_ANNOTATION,
	Class = SCE_GROOVY_CLASS,
	Interface = SCE_GROOVY_INTERFACE,
	Trait = SCE_GROOVY_TRAIT,
	Enum = SCE_GROOVY_ENUM,
	Return = 0x40,
	While = 0x41,
};

static_assert(DefaultNestedStateBaseStyle + 1 == SCE_GROOVY_STRING_DQ);
static_assert(DefaultNestedStateBaseStyle + 2 == SCE_GROOVY_TRIPLE_STRING_DQ);
static_assert(DefaultNestedStateBaseStyle + 3 == SCE_GROOVY_SLASHY_STRING);
static_assert(DefaultNestedStateBaseStyle + 4 == SCE_GROOVY_DOLLAR_SLASHY);

constexpr bool IsGroovyIdentifierStart(int ch) noexcept {
	return IsIdentifierStartEx(ch) || ch == '$';
}

constexpr bool IsGroovyIdentifierChar(int ch) noexcept {
	return IsIdentifierCharEx(ch);
}

constexpr bool IsInterpolatedString(int state) noexcept {
	return state < SCE_GROOVY_STRING_SQ;
}

constexpr int GetStringQuote(int state) noexcept {
	return (state < SCE_GROOVY_STRING_SQ) ? '\"' : '\'';
}

constexpr bool IsTripleString(int state) noexcept {
	if constexpr (SCE_GROOVY_TRIPLE_STRING_DQ & 1) {
		return state & true;
	} else {
		return (state & 1) == 0;
	}
}

constexpr bool IsSpaceEquiv(int state) noexcept {
	return state <= SCE_GROOVY_TASKMARKER;
}

constexpr bool FollowExpression(int chPrevNonWhite, int stylePrevNonWhite) noexcept {
	return chPrevNonWhite == ')' || chPrevNonWhite == ']'
		|| (stylePrevNonWhite >= SCE_GROOVY_OPERATOR_PF && stylePrevNonWhite <= SCE_GROOVY_NUMBER)
		|| IsGroovyIdentifierChar(chPrevNonWhite);
}

constexpr bool IsSlashyStringStart(int chPrevNonWhite, int stylePrevNonWhite) noexcept {
	// TODO: avoid colouring remaining document on typing `/` for line/block comment.
	return stylePrevNonWhite == SCE_GROOVY_WORD || !FollowExpression(chPrevNonWhite, stylePrevNonWhite);
}

void ColouriseGroovyDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initStyle, LexerWordList keywordLists, Accessor &styler) {
	int lineStateLineType = 0;

	KeywordType kwType = KeywordType::None;
	int chBeforeIdentifier = 0;

	std::vector<int> nestedState; // string interpolation "${}"
	int bracketCount = 0; // for () and []

	int visibleChars = 0;
	int chBefore = 0;
	int visibleCharsBefore = 0;
	int chPrevNonWhite = 0;
	int stylePrevNonWhite = SCE_GROOVY_DEFAULT;
	DocTagState docTagState = DocTagState::None;
	EscapeSequence escSeq;

	StyleContext sc(startPos, lengthDoc, initStyle, styler);
	if (sc.currentLine > 0) {
		int lineState = styler.GetLineState(sc.currentLine - 1);
		/*
		2: lineStateLineType
		3: nestedState count
		3*4: nestedState
		*/
		bracketCount = (lineState >> 2) & 0x3f;
		lineState >>= 8;
		if (lineState) {
			UnpackLineState(lineState, nestedState);
		}
	}
	if (startPos == 0) {
		if (sc.Match('#', '!')) {
			// Shell Shebang at beginning of file
			sc.SetState(SCE_GROOVY_COMMENTLINE);
			sc.Forward();
			lineStateLineType = GroovyLineStateMaskLineComment;
		}
	} else if (IsSpaceEquiv(initStyle)) {
		// look back for better slashy string colouring
		LookbackNonWhite(styler, startPos, SCE_GROOVY_TASKMARKER, chPrevNonWhite, stylePrevNonWhite);
	}

	while (sc.More()) {
		switch (sc.state) {
		case SCE_GROOVY_OPERATOR:
		case SCE_GROOVY_OPERATOR2:
		case SCE_GROOVY_OPERATOR_PF:
			sc.SetState(SCE_GROOVY_DEFAULT);
			break;

		case SCE_GROOVY_NUMBER:
			if (!IsDecimalNumberEx(sc.chPrev, sc.ch, sc.chNext)) {
				sc.SetState(SCE_GROOVY_DEFAULT);
			}
			break;

		case SCE_GROOVY_IDENTIFIER:
		case SCE_GROOVY_ANNOTATION:
		case SCE_GROOVY_ATTRIBUTE_AT:
			if (!IsGroovyIdentifierChar(sc.ch)) {
				if (sc.state == SCE_GROOVY_ANNOTATION) {
					if (sc.ch == '.') {
						sc.SetState(SCE_GROOVY_OPERATOR);
						sc.ForwardSetState(SCE_GROOVY_ANNOTATION);
						continue;
					}
				} else if (sc.state == SCE_GROOVY_IDENTIFIER) {
					char s[MaxKeywordSize];
					sc.GetCurrent(s, sizeof(s));
					if (s[0] == '@') {
						if (StrEqual(s, "@interface")) {
							sc.ChangeState(SCE_GROOVY_WORD);
							kwType = KeywordType::Annotation;
						} else {
							sc.ChangeState(SCE_GROOVY_ANNOTATION);
							continue;
						}
					} else if (keywordLists[KeywordIndex_Keyword].InList(s)) {
						const bool demoted = chBefore == '.' || chBefore == '&';
						sc.ChangeState(demoted ? SCE_GROOVY_WORD_DEMOTED : SCE_GROOVY_WORD);
						if (!demoted) {
							if (StrEqual(s, "import")) {
								if (visibleChars == sc.LengthCurrent()) {
									lineStateLineType = GroovyLineStateMaskImport;
								}
							} else if (StrEqualsAny(s, "class", "new", "extends", "instanceof", "throws", "as")) {
								kwType = KeywordType::Class;
							} else if (StrEqualsAny(s, "interface", "implements")) {
								kwType = KeywordType::Interface;
							} else if (StrEqual(s, "trait")) {
								kwType = KeywordType::Trait;
							} else if (StrEqual(s, "enum")) {
								kwType = KeywordType::Enum;
							} else if (StrEqual(s, "return")) {
								kwType = KeywordType::Return;
							} else if (StrEqualsAny(s, "if", "while")) {
								// to avoid treating following code as type cast:
								// if (identifier) expression, while (identifier) expression
								kwType = KeywordType::While;
							}
							if (kwType > KeywordType::None && kwType < KeywordType::Return) {
								const int chNext = sc.GetDocNextChar();
								if (!IsGroovyIdentifierStart(chNext)) {
									kwType = KeywordType::None;
								}
							}
						}
					} else if (keywordLists[KeywordIndex_Type].InList(s)) {
						sc.ChangeState(SCE_GROOVY_WORD2);
					} else if (keywordLists[KeywordIndex_Class].InList(s)) {
						sc.ChangeState(SCE_GROOVY_CLASS);
					} else if (keywordLists[KeywordIndex_Interface].InList(s)) {
						sc.ChangeState(SCE_GROOVY_INTERFACE);
					} else if (keywordLists[KeywordIndex_Enumeration].InList(s)) {
						sc.ChangeState(SCE_GROOVY_ENUM);
					} else if (keywordLists[KeywordIndex_Constant].InList(s)) {
						sc.ChangeState(SCE_GROOVY_CONSTANT);
					} else if (escSeq.outerState == SCE_GROOVY_DEFAULT && sc.ch == ':') {
						if (sc.chNext == ':') {
							// type::method
							sc.ChangeState(SCE_GROOVY_CLASS);
						} else if (bracketCount == 0 && nestedState.empty() && !AnyOf(chBefore, ',', '{', '(') && visibleChars == sc.LengthCurrent()) {
							const int chNext = sc.GetDocNextChar(true);
							if (IsADigit(chNext) || chNext == '\'' || chNext == '\"') {
								sc.ChangeState(SCE_GROOVY_PROPERTY);
							} else {
								sc.ChangeState(SCE_GROOVY_LABEL);
							}
						} else {
							sc.ChangeState(SCE_GROOVY_PROPERTY);
						}
					} else if (escSeq.outerState == SCE_GROOVY_DEFAULT && sc.ch != '.') {
						if (kwType > KeywordType::None && kwType < KeywordType::Return) {
							sc.ChangeState(static_cast<int>(kwType));
						} else {
							const int chNext = sc.GetDocNextChar(sc.ch == ')');
							if (sc.ch == ')') {
								if (chBeforeIdentifier == '(' && (chNext == '(' || (kwType != KeywordType::While && IsGroovyIdentifierChar(chNext)))) {
									// (type)(expression)
									// (type)expression, (type)++identifier, (type)--identifier
									sc.ChangeState(SCE_GROOVY_CLASS);
								}
							} else if (chNext == '(' || IsADigit(chNext) || chNext == '\'' || chNext == '"') {
								// property value
								// method parameter
								if (chNext == '(' && kwType != KeywordType::Return && (IsGroovyIdentifierChar(chBefore) || chBefore == ']')) {
									// type method()
									// type[] method()
									// type<type> method()
									sc.ChangeState(SCE_GROOVY_FUNCTION_DEFINITION);
								} else {
									sc.ChangeState(SCE_GROOVY_FUNCTION);
								}
							} else if (sc.Match('[', ']') || sc.Match('.', '&')
								|| (sc.ch == '<' && (sc.chNext == '>' || sc.chNext == '?'))
								|| (chBeforeIdentifier == '<' && (chNext == '>' || chNext == '<'))) {
								// type[] identifier
								// type.&method
								// type<>, type<?>, type<? super T>
								// type<type>
								// type<type<type>>
								// type<type, type>
								// class type implements interface, interface {}
								sc.ChangeState(SCE_GROOVY_CLASS);
							} else if (IsGroovyIdentifierStart(chNext)) {
								// type identifier
								// method parameter
								sc.ChangeState(IsLowerCase(s[0]) ? SCE_GROOVY_FUNCTION : SCE_GROOVY_CLASS);
							} else if (chNext == '{' && IsLowerCase(s[0])) {
								// block {}
								sc.ChangeState(SCE_GROOVY_ACTION);
							}
						}
					}
					stylePrevNonWhite = sc.state;
					if (sc.state != SCE_GROOVY_WORD && sc.ch != '.') {
						kwType = KeywordType::None;
					}
					if (escSeq.outerState != SCE_GROOVY_DEFAULT) {
						if (sc.ch == '.' && sc.chNext != '$' && IsGroovyIdentifierStart(sc.chNext)) {
							sc.SetState(SCE_GROOVY_OPERATOR2);
							sc.ForwardSetState(SCE_GROOVY_IDENTIFIER);
						} else {
							sc.SetState(escSeq.outerState);
						}
						continue;
					}
				}
				sc.SetState(SCE_GROOVY_DEFAULT);
			}
			break;

		case SCE_GROOVY_COMMENTLINE:
		case SCE_GROOVY_COMMENTBLOCK:
		case SCE_GROOVY_COMMENTBLOCKDOC:
			if (sc.atLineStart && (sc.state == SCE_GROOVY_COMMENTLINE)) {
				sc.SetState(SCE_GROOVY_DEFAULT);
				break;
			}
			switch (docTagState) {
			case DocTagState::At:
				docTagState = DocTagState::None;
				break;
			case DocTagState::InlineAt:
				if (sc.ch == '}') {
					docTagState = DocTagState::None;
					sc.SetState(SCE_GROOVY_COMMENTTAGAT);
					sc.ForwardSetState(SCE_GROOVY_COMMENTBLOCKDOC);
				}
				break;
			case DocTagState::TagOpen:
			case DocTagState::TagClose:
				if (sc.Match('/', '>') || sc.ch == '>') {
					docTagState = DocTagState::None;
					sc.SetState(SCE_GROOVY_COMMENTTAGHTML);
					sc.Forward((sc.ch == '/') ? 2 : 1);
					sc.SetState(SCE_GROOVY_COMMENTBLOCKDOC);
				}
				break;
			default:
				break;
			}
			if ((sc.state == SCE_GROOVY_COMMENTBLOCK || sc.state == SCE_GROOVY_COMMENTBLOCKDOC) && sc.Match('*', '/')) {
				sc.Forward();
				sc.ForwardSetState(SCE_GROOVY_DEFAULT);
				break;
			}
			if (docTagState == DocTagState::None && sc.state == SCE_GROOVY_COMMENTBLOCKDOC) {
				if (sc.ch == '@' && IsAlpha(sc.chNext) && IsCommentTagPrev(sc.chPrev)) {
					docTagState = DocTagState::At;
					sc.SetState(SCE_GROOVY_COMMENTTAGAT);
				} else if (sc.Match('{', '@') && IsAlpha(sc.GetRelative(2))) {
					docTagState = DocTagState::InlineAt;
					sc.SetState(SCE_GROOVY_COMMENTTAGAT);
					sc.Forward();
				} else if (sc.ch == '<') {
					if (IsAlpha(sc.chNext)) {
						docTagState = DocTagState::TagOpen;
						sc.SetState(SCE_GROOVY_COMMENTTAGHTML);
					} else if (sc.chNext == '/' && IsAlpha(sc.GetRelative(2))) {
						docTagState = DocTagState::TagClose;
						sc.SetState(SCE_GROOVY_COMMENTTAGHTML);
						sc.Forward();
					}
				}
			}
			if (docTagState == DocTagState::None && HighlightTaskMarker(sc, visibleChars, visibleCharsBefore, SCE_GROOVY_TASKMARKER)) {
				continue;
			}
			break;

		case SCE_GROOVY_COMMENTTAGAT:
		case SCE_GROOVY_COMMENTTAGHTML:
			if (!(IsIdentifierChar(sc.ch) || sc.ch == '-' || sc.ch == ':')) {
				sc.SetState(SCE_GROOVY_COMMENTBLOCKDOC);
				continue;
			}
			break;

		case SCE_GROOVY_STRING_SQ:
		case SCE_GROOVY_TRIPLE_STRING_SQ:
		case SCE_GROOVY_STRING_DQ:
		case SCE_GROOVY_TRIPLE_STRING_DQ:
		case SCE_GROOVY_SLASHY_STRING:
		case SCE_GROOVY_DOLLAR_SLASHY:
			if (sc.atLineStart && (sc.state == SCE_GROOVY_STRING_SQ || sc.state == SCE_GROOVY_STRING_DQ)) {
				sc.SetState(SCE_GROOVY_DEFAULT);
			} else if (sc.ch == '\\') {
				if (escSeq.resetEscapeState(sc.state, sc.chNext)) {
					sc.SetState(SCE_GROOVY_ESCAPECHAR);
					sc.Forward();
				}
			} else if (sc.ch == '$' && IsInterpolatedString(sc.state)) {
				escSeq.outerState = sc.state;
				sc.SetState(SCE_GROOVY_OPERATOR2);
				sc.Forward();
				if ((sc.ch == '$' || sc.ch == '/') && escSeq.outerState == SCE_GROOVY_DOLLAR_SLASHY) {
					escSeq.digitsLeft = 1;
					sc.ChangeState(SCE_GROOVY_ESCAPECHAR);
				} else if (sc.ch == '{') {
					nestedState.push_back(escSeq.outerState);
				} else if (sc.ch != '$' && IsGroovyIdentifierStart(sc.ch)) {
					chBefore = '.'; // demoted
					sc.SetState(SCE_GROOVY_IDENTIFIER);
				} else { // error
					sc.SetState(escSeq.outerState);
					continue;
				}
			} else if (sc.state == SCE_GROOVY_SLASHY_STRING || sc.state == SCE_GROOVY_DOLLAR_SLASHY) {
				if (sc.ch == '/' && (sc.state != SCE_GROOVY_DOLLAR_SLASHY || sc.chNext == '$')) {
					if (sc.state == SCE_GROOVY_DOLLAR_SLASHY) {
						sc.Forward();
					}
					sc.ForwardSetState(SCE_GROOVY_DEFAULT);
				}
			} else if (sc.ch == GetStringQuote(sc.state) && (!IsTripleString(sc.state) || sc.MatchNext())) {
				if (IsTripleString(sc.state)) {
					sc.Advance(2);
				}
				sc.ForwardSetState(SCE_GROOVY_DEFAULT);
			}
			break;

		case SCE_GROOVY_ESCAPECHAR:
			if (escSeq.atEscapeEnd(sc.ch)) {
				sc.SetState(escSeq.outerState);
				continue;
			}
			break;
		}

		if (sc.state == SCE_GROOVY_DEFAULT) {
			if (sc.ch == '/') {
				if (sc.chNext == '/') {
					visibleCharsBefore = visibleChars;
					sc.SetState(SCE_GROOVY_COMMENTLINE);
					if (visibleChars == 0) {
						lineStateLineType = GroovyLineStateMaskLineComment;
					}
				} else if (sc.chNext == '*') {
					visibleCharsBefore = visibleChars;
					docTagState = DocTagState::None;
					sc.SetState(SCE_GROOVY_COMMENTBLOCK);
					sc.Forward(2);
					if (sc.ch == '*' && sc.chNext != '*') {
						sc.ChangeState(SCE_GROOVY_COMMENTBLOCKDOC);
					}
					continue;
				} else {
					if (IsSlashyStringStart(chPrevNonWhite, stylePrevNonWhite)) {
						sc.SetState(SCE_GROOVY_SLASHY_STRING);
					} else {
						sc.SetState(SCE_GROOVY_OPERATOR);
					}
				}
			} else if (sc.ch == '\'' || sc.ch == '\"') {
				sc.SetState((sc.ch == '\'') ? SCE_GROOVY_STRING_SQ : SCE_GROOVY_STRING_DQ);
				if (sc.MatchNext()) {
					static_assert(SCE_GROOVY_TRIPLE_STRING_SQ - SCE_GROOVY_STRING_SQ == SCE_GROOVY_TRIPLE_STRING_DQ - SCE_GROOVY_STRING_DQ);
					sc.ChangeState(sc.state + SCE_GROOVY_TRIPLE_STRING_SQ - SCE_GROOVY_STRING_SQ);
					sc.Advance(2);
				}
			} else if (sc.Match('$', '/')) {
				sc.SetState(SCE_GROOVY_DOLLAR_SLASHY);
				sc.Forward();
			} else if (sc.ch == '+' || sc.ch == '-') {
				sc.SetState(SCE_GROOVY_OPERATOR);
				if (sc.ch == sc.chNext) {
					sc.ChangeState(SCE_GROOVY_OPERATOR_PF);
					sc.Forward();
				}
			} else if (IsADigit(sc.ch)) {
				sc.SetState(SCE_GROOVY_NUMBER);
			} else if (IsGroovyIdentifierStart(sc.ch)) {
				escSeq.outerState = SCE_GROOVY_DEFAULT;
				chBefore = (stylePrevNonWhite == SCE_GROOVY_FUNCTION) ? 0 : chPrevNonWhite;
				if (chPrevNonWhite != '.') {
					chBeforeIdentifier = chPrevNonWhite;
				}
				sc.SetState(SCE_GROOVY_IDENTIFIER);
			} else if (sc.ch == '@' && IsGroovyIdentifierStart(sc.chNext)) {
				const int state = (sc.chPrev == '.') ? SCE_GROOVY_ATTRIBUTE_AT
						: ((sc.chNext == 'i') ? SCE_GROOVY_IDENTIFIER : SCE_GROOVY_ANNOTATION);
				sc.SetState(state);
			} else if (IsAGraphic(sc.ch)) {
				sc.SetState(SCE_GROOVY_OPERATOR);
				if (!nestedState.empty()) {
					sc.ChangeState(SCE_GROOVY_OPERATOR2);
					if (sc.ch == '{') {
						nestedState.push_back(SCE_GROOVY_DEFAULT);
					} else if (sc.ch == '}') {
						const int outerState = TakeAndPop(nestedState);
						sc.ForwardSetState(outerState);
						continue;
					}
				} else {
					if (sc.ch == '[' || sc.ch == '(') {
						++bracketCount;
					} else if (sc.ch == ']' || sc.ch == ')') {
						if (bracketCount > 0) {
							--bracketCount;
						}
					}
				}
			}
		}

		if (!isspacechar(sc.ch)) {
			visibleChars++;
			if (!IsSpaceEquiv(sc.state)) {
				chPrevNonWhite = sc.ch;
				stylePrevNonWhite = sc.state;
			}
		}
		if (sc.atLineEnd) {
			int lineState = lineStateLineType | (bracketCount << 2);
			if (!nestedState.empty()) {
				lineState |= PackLineState(nestedState) << 8;
			}
			styler.SetLineState(sc.currentLine, lineState);
			lineStateLineType = 0;
			visibleChars = 0;
			visibleCharsBefore = 0;
			docTagState = DocTagState::None;
			kwType = KeywordType::None;
		}
		sc.Forward();
	}

	sc.Complete();
}

struct FoldLineState {
	int lineComment;
	int packageImport;
	constexpr explicit FoldLineState(int lineState) noexcept:
		lineComment(lineState & GroovyLineStateMaskLineComment),
		packageImport((lineState >> 1) & 1) {
	}
};

void FoldGroovyDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initStyle, LexerWordList /*keywordLists*/, Accessor &styler) {
	const Sci_PositionU endPos = startPos + lengthDoc;
	Sci_Line lineCurrent = styler.GetLine(startPos);
	FoldLineState foldPrev(0);
	int levelCurrent = SC_FOLDLEVELBASE;
	if (lineCurrent > 0) {
		levelCurrent = styler.LevelAt(lineCurrent - 1) >> 16;
		foldPrev = FoldLineState(styler.GetLineState(lineCurrent - 1));
		const Sci_PositionU bracePos = CheckBraceOnNextLine(styler, lineCurrent - 1, SCE_GROOVY_OPERATOR, SCE_GROOVY_TASKMARKER);
		if (bracePos) {
			startPos = bracePos + 1; // skip the brace
		}
	}

	int levelNext = levelCurrent;
	FoldLineState foldCurrent(styler.GetLineState(lineCurrent));
	Sci_PositionU lineStartNext = styler.LineStart(lineCurrent + 1);

	int styleNext = styler.StyleIndexAt(startPos);
	int style = initStyle;
	int visibleChars = 0;

	while (startPos < endPos) {
		const int stylePrev = style;
		style = styleNext;
		styleNext = styler.StyleIndexAt(++startPos);

		switch (style) {
		case SCE_GROOVY_COMMENTBLOCK:
		case SCE_GROOVY_COMMENTBLOCKDOC:
		case SCE_GROOVY_TRIPLE_STRING_SQ:
		case SCE_GROOVY_TRIPLE_STRING_DQ:
		case SCE_GROOVY_SLASHY_STRING:
		case SCE_GROOVY_DOLLAR_SLASHY:
			if (style != stylePrev) {
				levelNext++;
			}
			if (style != styleNext) {
				levelNext--;
			}
			break;

		case SCE_GROOVY_OPERATOR:
		case SCE_GROOVY_OPERATOR2: {
			const char ch = styler[startPos - 1];
			if (ch == '{' || ch == '[' || ch == '(') {
				levelNext++;
			} else if (ch == '}' || ch == ']' || ch == ')') {
				levelNext--;
			}
		} break;
		}

		if (visibleChars == 0 && !IsSpaceEquiv(style)) {
			++visibleChars;
		}
		if (startPos == lineStartNext) {
			const FoldLineState foldNext(styler.GetLineState(lineCurrent + 1));
			levelNext = sci::max(levelNext, SC_FOLDLEVELBASE);
			if (foldCurrent.lineComment) {
				levelNext += foldNext.lineComment - foldPrev.lineComment;
			} else if (foldCurrent.packageImport) {
				levelNext += foldNext.packageImport - foldPrev.packageImport;
			} else if (visibleChars) {
				const Sci_PositionU bracePos = CheckBraceOnNextLine(styler, lineCurrent, SCE_GROOVY_OPERATOR, SCE_GROOVY_TASKMARKER);
				if (bracePos) {
					levelNext++;
					startPos = bracePos + 1; // skip the brace
					style = SCE_GROOVY_OPERATOR;
					styleNext = styler.StyleIndexAt(startPos);
				}
			}

			const int levelUse = levelCurrent;
			int lev = levelUse | (levelNext << 16);
			if (levelUse < levelNext) {
				lev |= SC_FOLDLEVELHEADERFLAG;
			}
			styler.SetLevel(lineCurrent, lev);

			lineCurrent++;
			lineStartNext = styler.LineStart(lineCurrent + 1);
			levelCurrent = levelNext;
			foldPrev = foldCurrent;
			foldCurrent = foldNext;
			visibleChars = 0;
		}
	}
}

}

extern const LexerModule lmGroovy(SCLEX_GROOVY, ColouriseGroovyDoc, "groovy", FoldGroovyDoc);
